<?php

namespace App\Console\Commands;

use App\Api\Controllers\Config\CqcPayConfigController;
use App\Common\RefundSuccessAction;
use App\Models\Order;
use App\Models\RefundOrder;
use App\Models\Store;
use App\Services\Pay\PayService;
use App\Services\PrintSqlService;
use App\Services\Store\StoreMchService;
use App\Services\Withhold\TL\BillLogService;
use App\Util\LockUtil;
use App\Util\SqlWhenUtil;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class WithholdTLCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'withhold:tl {day?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '通联代扣数据整理-用于定时任务脚本，每天早上09:20整理数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '512M');
        ini_set('max_execution_time', 0);
        try {
            $startTime = time();
            Log::channel("withhold")->info("每日通联代扣数据归档开始");
            $yesterday = $this->argument('day');
            if (empty($yesterday)) {
                $whereDay = date("Y-m-d", strtotime("-1 day"));
                $fileDayName = date("Ymd", strtotime("-1 day"));
            } else {
                $whereDay = date("Y-m-d", strtotime($yesterday . " 00:00:00"));
                $fileDayName = date("Ymd", strtotime($yesterday . " 00:00:00"));
            }
//            $yesterday = "2025-03-04";
            $where = [
                "institution_number" => "0705",
                "transaction_date" => $whereDay,
            ];
            $path = storage_path(config("withhold.tl.file_path"));
            if (!is_dir($path)) {
                mkdir($path, 0777, true);
            }
            $filePath = storage_path(config("withhold.tl.file_path") . '/' . $where["institution_number"] . "_" . $fileDayName . ".txt");
            if (file_exists($filePath)) {
                return;
            }
            //获取账单数据
            $service = new BillLogService();
//        PrintSqlService::listen();
            $service->tlRun($where, $where["institution_number"] . "_" . $fileDayName);
            $endTime = time();
            Log::channel("withhold")->info("每日通联代扣数据归档结束", [
                "success_second" => $endTime - $startTime
            ]);
        } catch (\Exception $e) {
            dump($e->getMessage() . "_" . $e->getFile() . "_" . $e->getLine());
            Log::channel("withhold")->info("每日通联代扣数据归档异常", [
                "error" => $e->getMessage() . "_" . $e->getFile() . "_" . $e->getLine()
            ]);
            //删除文件
            @unlink($filePath);
        }
    }
}
